package de.hbrs.ooka.entities;

import de.hbrs.ooka.entities.dtos.ConferenceTransferObject;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "conferenceentity")
@NamedQueries({
    @NamedQuery(name = "findConferenceByID", query = "SELECT c FROM ConferenceEntity c WHERE c.id = :id"),
    @NamedQuery(name = "findConferenceByName", query = "SELECT c FROM ConferenceEntity c WHERE c.m_title = :name"),
    @NamedQuery(name = "findAllConferences", query = "SELECT c FROM ConferenceEntity c"),})
public class ConferenceEntity implements ConferenceTransferObject, Serializable {

    private static final long serialVersionUID = 1L;
    private static long ID_COUNTER = 0;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String m_title = "";
    private String m_location = "";
    private String m_date = "";
    private int m_maxParticipants = Integer.MAX_VALUE;
    @ManyToOne
    private UserEntity m_organizer = null;
    @ManyToMany
    private List<UserEntity> m_participants = new ArrayList<UserEntity>();
    @OneToMany
    private List<PaperEntity> m_submittedPapers = new ArrayList<PaperEntity>();
    @ElementCollection
    private Map<Integer, Double> m_reputationlist = new HashMap<Integer, Double>();

    public ConferenceEntity() {
    }

    public ConferenceEntity(String name, String location, String date, int maxParticipants) {
        this.m_title = name;
        this.m_location = location;
        this.m_date = date;
        this.m_maxParticipants = maxParticipants;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return this.m_title;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (new Integer(id)).hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof ConferenceEntity)) {
            return false;
        }
        ConferenceEntity other = (ConferenceEntity) object;
        return this.id == other.id;
    }

    @Override
    public String toString() {
        return "de.hbrs.ooka.beans.entities.ConferenceEntity[ id=" + id + " ]";
    }

    public void setOrganizer(UserEntity orga) {
        if (this.m_organizer != null) {
            throw new UnsupportedOperationException("Organizer already defined!");
        }

        this.m_organizer = orga;
    }

    @Override
    public UserEntity getOrganizer() {
        return this.m_organizer;
    }

    public void addParticipant(UserEntity p) throws IllegalArgumentException {
        if (this.m_participants.size() >= this.m_maxParticipants) {
            throw new IllegalArgumentException(this.m_participants.size() + " >= " + this.m_maxParticipants);
        }
        this.m_participants.add(p);
    }

    @Override
    public List<UserEntity> getParticipants() {
        return this.m_participants;
    }

    public Map<Integer, Double> getReputationList() {
        return this.m_reputationlist;
    }

    public void setReputationList(Map<Integer, Double> m) {
        this.m_reputationlist = m;
    }

    @Override
    public double getReputation() {
        double sum = 0.0;
        for (Entry<Integer, Double> e : this.m_reputationlist.entrySet()) {
            sum += e.getValue().doubleValue();
        }

        return (this.m_reputationlist.size() > 0 ? sum / (double) this.m_reputationlist.size() : sum);
    }

    public void addRating(UserEntity u, double rating) {
        int index = this.m_participants.indexOf(u);
        if (index == -1) {
            throw new IllegalArgumentException("User not in list of participants: " + u.getName());
        }

        this.m_reputationlist.put(index, rating);
    }

    @Override
    public int getMaxParticipants() {
        return this.m_maxParticipants;
    }

    public void setMaxParticipants(int max) {
        this.m_maxParticipants = max;
    }

    @Override
    public String getDate() {
        return this.m_date;
    }

    public void setDate(String date) {
        this.m_date = date;
    }

    @Override
    public String getLocation() {
        return this.m_location;
    }

    public void setLocation(String location) {
        this.m_location = location;
    }

    @Override
    public String getTitle() {
        return this.m_title;
    }

    public void setTitle(String title) {
        this.m_title = title;
    }

    public void addPaper(PaperEntity paper) {
        this.m_submittedPapers.add(paper);
    }

    @Override
    public List<PaperEntity> getPapers() {
        return this.m_submittedPapers;
    }

    public void setPapers(List<PaperEntity> papers) {
        this.m_submittedPapers = papers;
    }
}
